<?php
@session_start();

$command = isset($_REQUEST['command'])? $_REQUEST['command'] : false;

// delete function
if($command && $command == "delete")
{
    $app_id = $_REQUEST['app_id'];
    unset($_SESSION['cart'][$app_id]);
}

// huy toan bo gio hang
if($command && $command == "destroy")
{
    // @session_destroy();
    $_SESSION['cart'] = array();
    header("location: cart.php");
    exit();
}

// xu ly viec update gio hang
if($command && $command == "update")
{
    if(isset($_POST['quantity']))
    {
       $quantity = $_POST['quantity']; // bien nay la mang vi khai bao [name]="variable[]"

        if(is_array($quantity))
        {
            foreach ($quantity as $k => $v) {

                if(isset($_SESSION['cart'][$k])) {
                    $_SESSION['cart'][$k]['so_luong'] = $v;
                }
            }
        }
    }
}
if($command && $command == "pay")
{

    header("location: payment.php");
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8"/>
    <title>Shopping cart</title>
    <link rel="stylesheet" type="text/css" href="../../assets/css/style.css"/>
    <script type="text/javascript">

        function update_cart()
        {
            if(confirm("Cập nhật giỏ hàng!"))
            {
                document.form1.command.value = "update";
                document.form1.submit();
            }
        }

        function delete_cart_with_id(id)
        {
            if(confirm("Bạn có muốn xóa sản phẩm này trong giỏ hàng không?"))
            {
                document.form1.command.value = "delete";
                document.form1.app_id.value = id;
                document.form1.submit();
            }
        }

        function destroy_cart()
        {
            if(confirm("Bạn có muốn xóa toàn bộ giỏ hàng không?"))
            {
                window.location.href = location.href + "?command=destroy";
            }
        }

        function billing_cart()
        {
            if(window.confirm("Bạn chọn chức năng thành toán giỏ hàng?"))
            {
                document.form1.command.value = "pay";
                document.form1.submit();
            }
        }
    </script>

</head>
<body>

<div id="wrapper">
    <div>


    <form action="" method="post" name="form1" class="form-group">

    <input type="hidden" name="command"/>
    <input type="hidden" name="app_id"/>
    <table border='0' class="table table-striped">
        <thead>
            <th>Hình ảnh</th>
            <th>Tên ứng dụng</th>
            <th>License</th>
            <th>Giá</th>
            <th>Số lượng</th>
            <th>Tổng</th>
            <th>Tác vụ</th>
        </thead>

        <tbody>
        <?php
            $sumTotal = 0;
            if(isset($_SESSION['cart']))
            {
                //$sumTotal = 0;
        ?>
        <?php foreach ($_SESSION['cart'] as $key=>$value): ?>
            <tr>
                <?php $id = $key;?>
                <td><img src="../../assets/images-gui/<?=$value['image']?>" alt="software" style="width:50px; height: 50px"/></td>
                <td><?php echo $value['ten_app']; ?></td>
                <td><?php echo $value['license']; ?></td>
                <td><?php echo $value['gia_app']; ?></td>
                <td><input type="text" value="<?=$value['so_luong']?>" name="quantity[<?=$key?>]"</td>
                <td><?php echo $value['gia_app']*$value['so_luong']?>$</td>
                <td><input type="button" class="btn btn-warning btn-xs" value="remove" onclick="delete_cart_with_id(<?=$id?>)" /></td>
            </tr>
        <?php
            $sumTotal += $value['gia_app']*$value['so_luong'];
            endforeach; ?>
        <?php
            }
        ?>
        </tbody>
    </table>

    <!-- tong tien -->
    <p style=" width:100%; padding: 7px; background-color:#ffffff; box-shadow: 0 1px 2px 0px; text-align: right;"><span style="margin-right: 160px;">Tổng tiền là <?=$sumTotal?>$</span></p>
    <?php $_SESSION['tong_tien'] = $sumTotal; ?>
    <input type="button" value="update" class="btn btn-default btn-xs"onclick="update_cart()"/> <!-- END update -->

    <input type="button" value="Destroy Cart" class="btn btn-default btn-xs" onclick="destroy_cart()"/> <!-- END destroyCart -->

    <input type="button" value="Billing" class="btn btn-default btn-xs" onclick="billing_cart()"/> <!-- END billingCart -->
    </form>
    <a href="../../index.php">Continue...</a>
    </div>
    <?php include("../../footer.php"); ?>
</div> <!-- END wrapper -->


</body>

</html>

